import axios from "axios"
import router from "../router"

axios.defaults.timeout = 10000;

axios.interceptors.request.use(config => {
	// 检测网络状态
	if(!window.navigator.onLine) {
        alert("网路已断开，请重新连接...")
        localStorage.removeItem("username")
        localStorage.removeItem("isAdmin")
        localStorage.removeItem("token")
		return;
	}
	
    if(router.currentRoute.path === "/mine/login") {
        if(localStorage.getItem("token")) {
            config.headers["Authorization"] = localStorage.getItem("token")
        }
    }
    // console.log("config: ",config)
    return config
}, error => {
    console.log(error);
    return Promise.reject(error)
})

axios.interceptors.response.use(res => {
    console.log("res: ", res)
    return res
}, error => {
    console.log(error);
    return Promise.reject(error)
})

export default axios